package com.video.myapplication.adapter;

import android.content.Context;
import android.media.Image;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.video.myapplication.R;
import com.video.myapplication.util.FileUtil;
import com.video.myapplication.util.TimeUtil;
import com.video.myapplication.util.Video;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.security.auth.login.LoginException;

public class FileAdapter extends RecyclerView.Adapter<FileAdapter.ViewHolder> {


    private Context mContext;
    private List<Video> dataSource;
    private HashMap<Integer,Boolean> selectItem;
    private List<Integer> selectIndex;

    public FileAdapter(Context context){
        this.mContext = context;
        this.mContext.getContentResolver();
        dataSource = new ArrayList<>();
        selectItem = new HashMap<>();
    }

    private void initData(){
        int len = dataSource.size();
        for(int i = 0;i < len; i++) {
            selectItem.put(i,false);
        }
    }

    private void updateDataSelect(){
        selectIndex = new ArrayList<>();
        int len = dataSource.size();
        for(int i = 0;i < len; i++) {
            boolean status = selectItem.get(i);
            if(status){
                selectIndex.add(i);
            }
        }
    }

    public int getSelectIndex(){
        int len = dataSource.size();
        for(int i = 0;i < len; i++) {
            boolean status = selectItem.get(i);
            if(status){
                return i;
            }
        }
        return 0;
    }

    private void updateViewStatus(){
        int len = selectIndex.size();
        for(int i = 0;i < len; i++){
            notifyItemChanged(selectIndex.get(i));
        }
    }


    public void setDataSource(List<Video> dataSource){
        this.dataSource = dataSource;
        initData();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.file_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.textView.setText(TimeUtil.formatDateTime(this.dataSource.get(position).getDuration() / 1000));
        holder.checkBox.setOnCheckedChangeListener(null);
        holder.checkBox.setChecked(selectItem.get(position));
        holder.thumbView.setImageBitmap(FileUtil.getVideoThumbnail(this.dataSource.get(position).getId(), mContext));
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateDataSelect();
                initData();
                updateViewStatus();
                selectItem.put(position, isChecked);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataSource.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        CheckBox checkBox;
        ImageView thumbView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.time_len);
            checkBox = itemView.findViewById(R.id.checkBox);
            thumbView = itemView.findViewById(R.id.thumbImage);
        }
    }
}



